import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;

public class Main extends JFrame {
    private JTable gradeTable;
    private DefaultTableModel tableModel;
    private JTextField studentIdField;
    private JTextField studentNameField;
    private JTextField courseIdField;
    private JTextField courseNameField;
    private JTextField scoreField;
    private List<Grade> grades = new ArrayList<>();
    private String filePath = "grades.txt";

    public Main() {
        setTitle("个人学习数据管理系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1000, 600);
        setLocationRelativeTo(null);
        // 表格相关
        String[] columnNames = {"学生ID", "学生姓名", "课程ID", "课程名称", "成绩"};
        tableModel = new DefaultTableModel(columnNames, 0);
        gradeTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(gradeTable);

        // 输入框相关
        studentIdField = new JTextField(10);
        studentNameField = new JTextField(10);
        courseIdField = new JTextField(10);
        courseNameField = new JTextField(10);
        scoreField = new JTextField(10);

        JButton addButton = new JButton("添加成绩");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentId = studentIdField.getText();
                String studentName = studentNameField.getText();
                String courseId = courseIdField.getText();
                String courseName = courseNameField.getText();
                int score = Integer.parseInt(scoreField.getText());

                Student student = new Student(studentId, studentName);
                Course course = new Course(courseId, courseName);
                Grade grade = new Grade(student, course, score);
                grades.add(grade);

                updateTable();
                DataSaver.saveGradesToFile(grades, filePath);

                clearFields();
            }
        });

        JButton queryButton = new JButton("查询成绩");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                grades = DataLoader.loadGradesFromFile(filePath);
                updateTable();
            }
        });

        JPanel inputPanel = new JPanel();
        inputPanel.add(new JLabel("学生ID:"));
        inputPanel.add(studentIdField);
        inputPanel.add(new JLabel("学生姓名:"));
        inputPanel.add(studentNameField);
        inputPanel.add(new JLabel("课程ID:"));
        inputPanel.add(courseIdField);
        inputPanel.add(new JLabel("课程名称:"));
        inputPanel.add(courseNameField);
        inputPanel.add(new JLabel("成绩:"));
        inputPanel.add(scoreField);
        inputPanel.add(addButton);
        inputPanel.add(queryButton);

        getContentPane().add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(inputPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    private void updateTable() {
        tableModel.setRowCount(0);
        for (Grade grade : grades) {
            Object[] row = {
                    grade.getStudent().getStudentId(),
                    grade.getStudent().getStudentName(),
                    grade.getCourse().getCourseId(),
                    grade.getCourse().getCourseName(),
                    grade.getScore()
            };
            tableModel.addRow(row);
        }
    }

    private void clearFields() {
        studentIdField.setText("");
        studentNameField.setText("");
        courseIdField.setText("");
        courseNameField.setText("");
        scoreField.setText("");
    }

    public static void main(String[] args) {
        new Main();
    }
}
    